package com.eqdd.yiqidian.ui.yiqixue;

import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;

import com.eqdd.yiqidian.LearnMainActivityCustom;
import com.eqdd.yiqidian.ListItem85Custom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.MyFragmentPagerAdapter;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.box.ItemDecoration.SectionDividerItemDecoration;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.ui.yiqixue.add.AddLectureActivity;
import com.eqdd.yiqidian.ui.yiqixue.add.AddVideoActivity;
import com.eqdd.yiqidian.ui.yiqixue.add.AddXueArticleActivity;
import com.eqdd.yiqidian.ui.yiqixue.fragment.CommendTeacherFragment;
import com.eqdd.yiqidian.ui.yiqixue.fragment.FindArticleFragment;
import com.eqdd.yiqidian.ui.yiqixue.fragment.FindLectureFragment;
import com.eqdd.yiqidian.ui.yiqixue.fragment.FindNeedFragment;
import com.eqdd.yiqidian.ui.yiqixue.fragment.FindVideoFragment;
import com.eqdd.yiqidian.ui.yiqixue.fragment.LearnMainFragment;
import com.eqdd.yiqidian.utils.ClickUtil;
import com.eqdd.yiqidian.utils.DensityUtil;
import com.xujiaji.happybubble.BubbleDialog;
import com.xujiaji.happybubble.BubbleLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * @author吕志豪 .
 * @date 17-12-29  下午4:57.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class LearnMainActivity extends BaseActivity {
    List<Fragment> mFragments;


    List<String> mTitles = new ArrayList<>();
    private LearnMainActivityCustom dataBinding;
    private BubbleDialog bubbleDialog;
    private Bundle bundle;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_learn_main);
        initTopTitleBar(View.VISIBLE, "易企学");
        dataBinding.includeTitle.ivRight.setVisibility(View.VISIBLE);
        dataBinding.includeTitle.ivSearch.setVisibility(View.VISIBLE);
        dataBinding.includeTitle.ivRight.setImageResource(R.mipmap.addg);
        ClickUtil.click(dataBinding.includeTitle.ivSearch, () -> {
            Bundle bundle = new Bundle();
            bundle.putInt(Config.POS, dataBinding.viewpager.getCurrentItem() == 0 ? 0 : (dataBinding.viewpager.getCurrentItem() - 1));
            startActivity(LearnSearchActivity.class, bundle);
        });
        initTopDialogAndClick();
    }

    private void initTopDialogAndClick() {
        if (bubbleDialog == null) {
            ListItem85Custom inflate = DataBindingUtil.inflate(LayoutInflater.from(this), R.layout.list_item_85, null, false);
            inflate.recyclerView.setLayoutManager(new LinearLayoutManager(this));
            inflate.recyclerView.addItemDecoration(new SectionDividerItemDecoration(this)
                    .setLeftPadding(DensityUtil.percentW(20))
                    .setRightPadding(DensityUtil.percentW(20))
                    .setLastBootomHeght(0)
                    .setHeadHeight(0));
            ArrayList<String> options = new ArrayList<>();
            options.add("个人信息");
            options.add("发视频");
            options.add("发课程");
            options.add("发文章");
            SlimAdapterEx.create().register(R.layout.list_item_86, new SlimInjector<String>() {
                @Override
                public void onInject(String data, IViewInjector injector) {
                    injector.text(R.id.tv_content, data);
                }
            }).attachTo(inflate.recyclerView).updateData(options);
            ItemClickSupport.addTo(inflate.recyclerView)
                    .setOnItemClickListener((recyclerView, position, v) -> {
                        bubbleDialog.hide();
                        switch (position) {
                            case 0:
                                startActivity(LearnMeActivity.class);
                                break;
                            case 1:
                                startActivity(AddVideoActivity.class);
                                break;
                            case 2:
                                startActivity(AddLectureActivity.class);
                                break;
                            case 3:
                                startActivity(AddXueArticleActivity.class);
                                break;
                        }
                    });
            BubbleLayout bl = new BubbleLayout(this);
            bl.setBubbleColor(Color.WHITE);
            bl.setShadowColor(Color.LTGRAY);
            bl.setLookLength(DensityUtil.percentW(14));
            bl.setLookWidth(DensityUtil.percentW(10));
            bubbleDialog = new BubbleDialog(this)
                    .setBubbleLayout(bl)
                    .addContentView(inflate.getRoot())
                    .setClickedView(dataBinding.includeTitle.ivRight)
                    .setPosition(BubbleDialog.Position.BOTTOM)
                    .calBar(true);
            ClickUtil.click(dataBinding.includeTitle.ivRight, () -> {
                bubbleDialog.show();
            });
        }
    }

    @Override
    public void initData() {
        mTitles.add("首页");
        mTitles.add("找讲师");
        mTitles.add("选课程");
        mTitles.add("看视频");
        mTitles.add("品文章");
//        mTitles.add("找需求");
        setupViewPager();
    }

    @Override
    public void setView() {

    }

    private void setupViewPager() {
        setupViewPager(dataBinding.viewpager);
        dataBinding.slidingTabStrip.setViewPager(dataBinding.viewpager);
    }

    private void setupViewPager(ViewPager viewPager) {
        mFragments = new ArrayList<>();

        mFragments.add(new LearnMainFragment());
        addTeacherFragment();
        addLectureFragment();
        addVideoFragment();
        addArticleFragment();
//        addNeedFragment();
        viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), mTitles, mFragments));
    }

    private void addNeedFragment() {
        bundle = new Bundle();
        bundle.putString(Config.URL, HttpConfig.LEARN_NEED_LIST_MINE);
        FindNeedFragment findNeedFragment = new FindNeedFragment();
        findNeedFragment.setArguments(bundle);
        mFragments.add(findNeedFragment);
    }

    private void addTeacherFragment() {
        bundle = new Bundle();
        bundle.putString(Config.URL, HttpConfig.LEARN_TEACHER_COMMENT_LIST);
        CommendTeacherFragment commendTeacherFragment = new CommendTeacherFragment();
        commendTeacherFragment.setArguments(bundle);
        mFragments.add(commendTeacherFragment);
    }

    private void addArticleFragment() {
        bundle = new Bundle();
        bundle.putString(Config.URL, HttpConfig.LEARN_ARTICLE_LIST);
        FindArticleFragment findArticleFragment = new FindArticleFragment();
        findArticleFragment.setArguments(bundle);
        findArticleFragment.refresh(HttpConfig.LEARN_ARTICLE_LIST, "");
        mFragments.add(findArticleFragment);
    }

    private void addVideoFragment() {
        bundle = new Bundle();
        bundle.putString(Config.URL, HttpConfig.LEARN_VIDEO_LIST);
        FindVideoFragment findVideoFragment = new FindVideoFragment();
        findVideoFragment.setArguments(bundle);
        mFragments.add(findVideoFragment);
    }

    private void addLectureFragment() {
        bundle = new Bundle();
        bundle.putString(Config.URL, HttpConfig.LEARN_LECTURE_LIST);
        FindLectureFragment findLectureFragment = new FindLectureFragment();
        findLectureFragment.setArguments(bundle);
        mFragments.add(findLectureFragment);
    }
}
